package com.example.firstdemo.network.repository;
import com.example.firstdemo.network.service.NetworkException;

sealed class ApiResult<out T> {
    data class Success<out T>(val data: T) : ApiResult<T>()
    data class Error(val code: Int, val message: String) : ApiResult<Nothing>()
    object Loading : ApiResult<Nothing>()
}

suspend fun <T> withApiErrorHandling(block: suspend () -> T): ApiResult<T> {
    return try {
        ApiResult.Success(block())
    } catch (e: NetworkException) {
        ApiResult.Error(e.code, e.message ?: "")
    }
}